package java6681.selection;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Random;

public class SelectionWin6681 extends Application {
    private Label label;
    private Label lblSelection;
    private Object[] list;

    @Override
    public void start(Stage primaryStage) {
        VBox root = new VBox(30);
        Button button = new Button("随机选人");

        label = new Label("212006681|张舒婷");
        lblSelection = new Label("恭喜学号和姓名被选中");

        root.getChildren().add(label);
        root.getChildren().add(button);
        root.getChildren().add(lblSelection);
        root.setAlignment(Pos.CENTER);

        Scene scene = new Scene(root, 400, 300);

        primaryStage.setScene(scene);
        primaryStage.setTitle("随机选人");
        primaryStage.show();

        try {
            list = Files.lines(Paths.get("data/Student.txt")).toArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        EventHandler<ActionEvent> listener = new MyEvenHandler();
        button.setOnAction(listener);
    }

    class MyEvenHandler implements EventHandler<ActionEvent> {

        @Override
        public void handle(ActionEvent event) {
            Random random = new Random();
            /*生成随机数*/
            int r = random.nextInt(list.length);
            /*从数组中读取元素*/
            Object result = list[r];
            /*显示结果*/
            lblSelection.setText((String) result);
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
